package com.wemei.auto

import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.Process
import androidx.multidex.MultiDex
import com.tencent.bugly.crashreport.CrashReport
import com.wemei.auto.api.core.ApiHelper
import com.wemei.auto.cp_fast_accessibility.FastAccessibilityService
import com.wemei.auto.ui.AppCore
import com.wemei.auto.ui.utils.Logger
import me.jessyan.autosize.AutoSize

class App : Application() {
    companion object {
        lateinit var app: App
    }

    override fun onCreate() {
        super.onCreate()
        val processName = getProcessName(this)
        if (packageName == processName) {
            app = this
            AppCore.init(this)
            init()
        }
    }

    private fun init() {
        FastAccessibilityService.init(this, MyAccessibilityService::class.java, arrayListOf())
        AutoSize.checkAndInit(this)
//        val debug = BuildConfig.DEBUG
        val debug = true
        ApiHelper.initHttp(debug)
        Logger.isDebug = debug
        CrashReport.initCrashReport(this, "1ae9d417e2", BuildConfig.DEBUG)
//        MLApplication.getInstance().apiKey =
//            "DAEDAGXXB4G84sgd3HaIm0QpzC0b6lIuems/IsiU57arDnIU/X16pr0qmr/zhxdPIy2SNpBC0oeLruKYMYf63NTzPuV66D6H3uorkA=="
    }

    /**
     * 初始化云推送通道
     */
    private fun initCloudChannel() {
//        PushServiceFactory.init(applicationContext)
//        val pushService = PushServiceFactory.getCloudPushService()
//        pushService.setLogLevel(CloudPushService.LOG_DEBUG)   //仅适用于Debug包，正式包不需要此行
//        pushService.register(applicationContext, object : CommonCallback, com.alibaba.sdk.android.push.CommonCallback {
//            override fun onSuccess(response: String?) {
//                Logger.e("init cloudchannel success；设备id：${pushService.deviceId}")
//            }
//
//            override fun onFailed(errorCode: String?, errorMessage: String?) {
//                Logger.e("init cloudchannel failed -- errorcode:$errorCode -- errorMessage:$errorMessage")
//            }
//        })
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            val mNotificationManager: NotificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
//            // 通知渠道的id。
//            val id = "push"
//            // 用户可以看到的通知渠道的名字。
//            val name: CharSequence = "深粮云推送"
//            // 用户可以看到的通知渠道的描述。
//            val description = "用户接收系统推送预警消息"
//            val importance: Int = NotificationManager.IMPORTANCE_HIGH
//            val mChannel = NotificationChannel(id, name, importance)
//            // 配置通知渠道的属性。
//            mChannel.description = description
//            // 设置通知出现时的闪灯（如果Android设备支持的话）。
//            mChannel.enableLights(true)
//            mChannel.lightColor = Color.RED
//            // 设置通知出现时的震动（如果Android设备支持的话）。
//            mChannel.enableVibration(true)
//            mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
//            // 最后在notificationmanager中创建该通知渠道。
//            mNotificationManager.createNotificationChannel(mChannel)
//        }
    }

    override fun attachBaseContext(context: Context?) {
        super.attachBaseContext(context)
        MultiDex.install(this)
//        System.loadLibrary("msaoaidsec")
    }

    private fun getProcessName(context: Context): String? {
        val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val runningApps = am.runningAppProcesses ?: return null
        for (proInfo in runningApps) {
            if (proInfo.pid == Process.myPid()) {
                if (proInfo.processName != null) {
                    return proInfo.processName
                }
            }
        }
        return null
    }
}